package com.example.coreshopapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coreshopapp.R;
import com.example.coreshopapp.bean.OrderDetailData;
import com.google.android.material.transition.Hold;

import java.util.ArrayList;
import java.util.List;

public class OrderDetailAdapter  extends RecyclerView.Adapter<OrderDetailAdapter.ViewHolder> {
    private List<OrderDetailData.OrderItem> goodsList = new ArrayList<>();

//    public OrderDetailAdapter(List<OrderDetailData.OrderItem> goodsList) {
//        this.goodsList = goodsList;
//    }
    public void updateOrderDetailAdapter(List<OrderDetailData.OrderItem> goodsList) {
        OrderDetailData.OrderItem orderItem = new OrderDetailData.OrderItem();
        this.goodsList.clear();
        this.goodsList.add(orderItem);
        this.goodsList.addAll(goodsList);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_detail, parent, false);
        return new ViewHolder(inflate);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position == 0) {
            holder.box.setBackgroundResource(R.color.gray_bg);
//            holder.delete.setText("操作");
            holder.name.setText("商品");
            holder.quantity.setText("数量");
            holder.amount.setText("价格");
        } else {
            OrderDetailData.OrderItem item = goodsList.get(position);
            holder.name.setText(item.getName());
            holder.quantity.setText(item.getNums() + "");
            holder.amount.setText(item.getAmount() + "");
        }

    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name,orderId,quantity,amount,delete;
        private LinearLayout box;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
//            orderId = itemView.findViewById(R.id.tv_order_id);
            amount = itemView.findViewById(R.id.tv_amount);
            quantity = itemView.findViewById(R.id.tv_quantity);
//            delete = itemView.findViewById(R.id.btn_delete);
            box = itemView.findViewById(R.id.box);
        }
    }
}
